package com.woniu.controller;

import ch.qos.logback.core.rolling.helper.IntegerTokenConverter;
import com.woniu.common.CommonResult;
import com.woniu.entity.UsedProduct;
import com.woniu.entity.UsedProductImgs;
import com.woniu.service.UsedProductImgsService;
import com.woniu.service.UsedProductService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品图片表(UsedProductImgs)表控制层
 *
 * @author makejava
 * @since 2020-03-23 09:48:24
 */
@Controller
@RequestMapping("/usedProductImgs")
public class UsedProductImgsController {
    /**
     * 服务对象
     */
    @Resource
    private UsedProductImgsService usedProductImgsService;

    @Resource
    private UsedProductService usedProductService;

    @GetMapping("/findImgsBytypeId/{typeId}")
    public @ResponseBody CommonResult findImgsBytypeId(@PathVariable("typeId") Integer typeId){
        //根据商品类型查找商品
        List<UsedProduct> usedProducts = usedProductService.findAllByTypeId(typeId);
        ArrayList<String> imgsAll = new ArrayList<>();
        for (UsedProduct u:usedProducts) {
            List<UsedProductImgs> imgs = usedProductImgsService.findImgsByProductId(u.getId());
            imgsAll.add(imgs.get(0).getImgUrl());
        }



        return CommonResult.success(imgsAll);
    }

    /**
     * 功能描述: 根据商品Id查询图片Url
     * @Param: [productId]
     * @Return: com.woniu.common.CommonResult
     * @Author: yanghan
     * @Date: 2020/3/29 0:53
     */
    @GetMapping("/findImgsByProductId/{productId}")
    public @ResponseBody CommonResult findImgsByProductId(@PathVariable("productId") Integer productId){
        try {
            List<UsedProductImgs> imgs = usedProductImgsService.findImgsByProductId(productId);
            UsedProductImgs img = null;
            if (imgs.size()>0){
                img  = imgs.get(0);
                return CommonResult.success(img);
            }else {
                return CommonResult.failed("失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("失败");
        }
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public UsedProductImgs selectOne(Integer id) {
        return this.usedProductImgsService.queryById(id);
    }

}